<?php
require_once 'libs/Nine/Controller/Action.php';
require_once 'modules/user/models/User.php';

class access_IndexController extends Nine_Controller_Action {
	/**
	 * The default action - show the access page
	 */
	public function indexAction() {
		$this->_redirect ( "access/index/login" );
	}
	
	/**
	 * The default action - show the home page
	 */
	public function loginAction() {
		$this->view->headTitle ( 'Login' );
		
		$objUser = new Models_User ();
		
		$loginError = false;
		$submitHandler = Nine_Registry::getAppBaseUrl () . "access/index/login";
		
		$params = $this->_request->getParams ();
		if (isset ( $params ['username'] ) && $params ['username'] != "") {
			if ($this->_request->isPost ()) {
				if ($this->auth->login ( $params ['username'], $params ['password'] )) {
					/**
					 * Update last login time
					 */
					$objUser->updateLastLogin ( $params ['username'] );
					
					$this->_redirect($this->_getCallBackUrl());
				} else {
					$loginError = true;
				}
			} 
		}
		$this->view->submitHandler = $submitHandler;
		$this->view->loginError = $loginError;
		$this->view->accessMessage = $this->session->accessMessage;
		$this->session->accessMessage = null;
		
	}
	/**
	 * get and run with submited data
	 */
	public function logoutAction() {
		$this->auth->logout ();
		$this->_redirect ( "" );
	}
}